package com.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.pojo.Users;

public class UserDemo {

	public static void main(String[] args) {
		// 创建User对象
		Users user = new Users(1,"zhangsan", "123");
		// 实例化Configuration
		Configuration configuration = new Configuration();
		// 加载hibernate.cfg.xml文件
		configuration.configure("/hibernate.cfg.xml");
		// 创建SessionFactory
		// Hibernate4.3创建SessionFactory的方式
		StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
		standardServiceRegistryBuilder.applySettings(configuration
				.getProperties());
		SessionFactory sessionFactory = configuration
				.buildSessionFactory(standardServiceRegistryBuilder.build());
		// 打开Session
		Session session = sessionFactory.openSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		// 持久化操作
		session.save(user);
		// 提交事务
		trans.commit();
		// 关闭Session
		session.close();
	}

}
